<?php

namespace Biz\service\springHomeDecActivity;

use Biz\model\springHomeDecActivity\ActivityProduct as mActivityProduct;
use Biz\model\springHomeDecActivity\WishList as mWishList;
use Biz\pockets\constInterface\SpringHomeDecConfig;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\IdParse;

class WishListService implements SpringHomeDecConfig
{
    /**
     * 获取活动商品
     * @return \string[][]
     */
    public static function getActivityProducts($ids = [])
    {
        $where['status'] = mActivityProduct::STATUS_NORMAL;
        $where['is_show'] = mActivityProduct::IS_SHOW_YES;
        if (!empty($ids)) {
            $where['product_id'] = $ids;
        }
        $products = [];
        $selectedFields = ['product_id', 'product_name', 'product_type', 'product_pic', 'product_value'];
        $result = mActivityProduct::dataSelect($where, $selectedFields);
        if ($result && is_array($result)) {
            shuffle($result);
            $products = $result;
        }

        return $products;
    }

    /**
     * 获取商品Map
     * @return array|bool
     */
    public static function getProductsMap($ids = [])
    {
        $where['status'] = mActivityProduct::STATUS_NORMAL;
        if (!empty($ids)) {
            $where['product_id'] = array_unique($ids);
        }
        $productsMap = [];
        $selectedFields = ['product_id', 'product_name', 'product_type', 'product_pic', 'product_value'];
        $result = mActivityProduct::dataSelect($where, $selectedFields);
        if ($result && is_array($result)) {
            $productsMap = mActivityProduct::dataMap($result, 'product_id', null);
        }
        return $productsMap;
    }

    /**
     * 获取用户许愿商品
     * @param $wishIds
     * @return array|bool
     */
    public static function getWishProductsByWishIds($wishIds, $fields = [])
    {
        $where['wish_id'] = $wishIds;
        $where['data_status'] = mWishList::STATUS_NORMAL;
        $wishProducts = [];
        $result = mWishList::dataSelect($where, $fields);
        if ($result && is_array($result)) {
            $productMap = self::getProductsMap();
            foreach ($result as $wishProduct) {
                if (isset($productMap[$wishProduct['product_id']])) {
                    $product = $productMap[$wishProduct['product_id']];
                    $wishProducts[$wishProduct['wish_id']][] = $product;
                }
            }
        }
        return $wishProducts;
    }

    /**
     * 获取用户许愿商品
     * @param $uid
     * @return array|bool
     */
    public static function getWishProducts($uid)
    {
        $where['uid'] = $uid;
        $where['data_status'] = mWishList::STATUS_NORMAL;
        $wishProducts = [];
        $result = mWishList::dataSelect($where);
        if ($result && is_array($result)) {
            $productMap = self::getProductsMap();
            foreach ($result as $wishProduct) {
                $product = $productMap[$wishProduct['product_id']];
                if ($product) {
                    $product['winning'] = $wishProduct['winning'];
                    if ($product['product_type'] == mActivityProduct::TYPE_SERVICE) {
                        $product['service_url'] = self::WISH_SERVICE_H5_URL;
                    }
                    $productId = $wishProduct['product_id'];
                    $product['statSign'] = basicStatSign::genStatSign(basicStatSign::ACTIVITY_SPRING_HOME_DEC_PRODUCT_2022,
                        [
                            basicStatSign::EXTEND_KEY_OBJ_ID => $productId,
                            basicStatSign::EXTEND_KEY_OBJ_UID => $uid,
                        ]
                    );
                    $wishProducts[] = $product;
                }
            }
        }
        return $wishProducts;
    }

    /**
     * 获取许愿商品
     * @param $wishId 许愿id
     * @return array|bool
     */
    public static function getWishProductsById($wishId)
    {
        $where['wish_id'] = $wishId;
        $where['data_status'] = mWishList::STATUS_NORMAL;
        $wishProducts = [];
        $result = mWishList::dataSelect($where);
        if ($result && is_array($result)) {
            $productMap = self::getProductsMap();
            foreach ($result as $wishProduct) {
                $product = $productMap[$wishProduct['product_id']];
                $wishProducts[] = $product;
            }
        }
        return $wishProducts;
    }

    /**
     * 许愿商品价格总和检查
     * @param $wishProductIds
     * @return bool
     * @throws \Exception
     */
    public static function checkWishProductsValue($wishProductIds)
    {
        $wishProductsValue = 0.00;
        $wishProducts = self::getProductsMap($wishProductIds);
        foreach ($wishProductIds as $wishProductId) {
            if (isset($wishProductsS[$wishProductId])) {
                $wishProductsValue += $wishProducts[$wishProductId]['value'];
            }
        }
        if ($wishProductsValue > self::WISH_PRODUCT_MAX_TOTAL_VALUE) {
            throw new \Exception("超过5万，厚米买不起啦");
        }
        return true;
    }

    /**
     * 检查许愿商品数量
     * @param $wishProductIds
     * @return bool
     * @throws \Exception
     */
    public static function checkWishProductsCount($wishProductIds)
    {
        if (count($wishProductIds) > self::WISH_PRODUCT_MAX_COUNT) {
            throw new \Exception("最多支持添加10件商品");
        }
        return true;
    }


    /**
     * 设置中奖商品
     * @param $wishId
     * @return bool|int
     */
    public static function setUserProductWinning($wishId)
    {
        $where['wish_id'] = $wishId;
        $where['winning'] = mWishList::WINNING_NO;
        $where['data_status'] = mWishList::STATUS_NORMAL;

        $data['winning'] = mWishList::WINNING_YES;
        $data['update_time'] = date('Y-m-d H:i:s', time());

        $nRet = mWishList::dataUpdate($data, $where);

        return $nRet;

    }


}